package com.example.xt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xt.common.Constants;
import com.example.xt.common.Result;
import com.example.xt.entity.Supplier;
import com.example.xt.service.ISupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    private ISupplierService supplierService;

    @PostMapping("/save")
    public Result save(@RequestBody Supplier supplier) {
        String suplier = supplier.getSupplier();
        String address = supplier.getAddress();
        String telephone = supplier.getTelephone();
        if (suplier != null && address != null && telephone != null) {
            return Result.success(supplierService.save(supplier));
        } else {
            return Result.error(Constants.CODE_400, "数据不能为空");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Supplier supplier) {
        String suplier = supplier.getSupplier();
        String address = supplier.getAddress();
        String telephone = supplier.getTelephone();
        if (suplier != null && address != null && telephone != null) {
            return Result.success(supplierService.saveOrUpdate(supplier));
        } else {
            return Result.error(Constants.CODE_400, "数据不能为空");
        }
    }

    @GetMapping("/list")
    public List<Supplier> findAll() {
        return supplierService.list();
    }

    @DeleteMapping("/{suoolier}")
    public boolean delete(@PathVariable String suoolier) {
        return supplierService.removeById(suoolier);
    }


    @GetMapping("/page")
    public Page<Supplier> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String supplier) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        if (!"".equals(supplier)) {
            queryWrapper.like("supplier", supplier);
        }
        return supplierService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

}

